package logic

import (
	"context"
	"novel/model/mysql"

	"novel/novelrpc/internal/svc"
	"novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NovelShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelShowLogic {
	return &NovelShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NovelShowLogic) NovelShow(in *novelrpc.NovelShowListRequest) (*novelrpc.NovelShowListResponse, error) {
	// todo: add your logic here and delete this line
	offset := (in.Page - 1) * in.Size

	var user []mysql.Novel
	tx := mysql.DB.Debug().Offset(int(offset)).Limit(int(in.Size))
	if in.CreatedAt == 1 {
		tx.Find("novel.created_at desc")
	}
	if in.CreatedAt == 2 {
		tx.Find("novel.created_at asc")
	}
	if in.Name == "" {
		tx.Where("novel.name like ?", "%"+in.Name+"%")
	}
	tx.Find(&user)

	var novel []*novelrpc.NovelShow

	for _, m := range user {
		list := novelrpc.NovelShow{
			Name:   m.Name,
			Types:  m.Types,
			Tile:   m.Tile,
			Ifor:   m.Ifor,
			UserId: int64(m.UserId),
		}
		novel = append(novel, &list)
	}
	return &novelrpc.NovelShowListResponse{
		List: novel,
	}, nil
}
